package File.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

/**
 * 文件工具类
 * @author wangbaojiao
 *
 */
public class FileUtil {

	public static BufferedReader getBufferedReader(String filePath, String charset) throws FileNotFoundException, UnsupportedEncodingException{
		FileInputStream fileInputStream = new FileInputStream(filePath);
		BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream, charset), 81920);
		return reader;
	}
	
	public static BufferedWriter getBufferedWriter(String filePath, String charset) throws FileNotFoundException, UnsupportedEncodingException{
		FileOutputStream fileOutputStream = new FileOutputStream(filePath);
		BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, charset));
		return bufferedWriter;
	}
	
	
	public static File getFile(String path){
		return new File(path);
	}
	
	public static boolean moveFileToDir(File source, File destDir){
		if(!canMove(source, destDir)) return false;
		String name = source.getName();
		File destFile = new File(destDir + File.separator + name);
		return source.renameTo(destFile);
	}
	
	public static boolean canMove(File source, File dest){
		boolean canRead = source.canRead();
		return canRead && dest.canWrite();
	}
}
